﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace Centrica.Task.Model.Test
{
    public class TaskProxy : Task
    {
        public TaskProxy() : base()
        {
        }

        public TaskProxy(Centrica.Task.Model.Task task)
        {
            this.Description = task.Description;
            this.ID = task.ID;
            this.Name = task.Name;
            this.Owner = task.Owner;
            this.Status = task.Status;
            this.StartDate = task.StartDate;
            this.FinishDate = task.FinishDate;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class TaskTestListViewModel
    {
        public static XmlDocument Xml
        {
            get
            {
                List<Task> tasks =
                    TestTaskProvider.StaticInstance()
                    .GetAllTasks();

                XDocument xDocument = new XDocument();

                XElement xInfo = new XElement(@"Info");
                xDocument.Add(xInfo);

                tasks.ForEach(t =>
                {
                    xInfo.Add(
                            new XElement(@"Item",
                                new XAttribute(@"Category", t.Description),
                                new XAttribute(@"Name", t.Name),
                                new XAttribute(@"Owner", t.Owner),
                                new XAttribute(@"Status", t.Status),
                                new XAttribute(@"__ID", t.ID)
                            ),
                            new XElement(@"Item",
                                new XAttribute(@"Category", t.Description),
                                new XAttribute(@"Owner", t.StartDate.ToString(@"dd MMM yyyy")),
                                new XAttribute(@"Status", t.FinishDate.ToString(@"dd MMM yyyy")),
                                new XAttribute(@"__ID", t.ID)
                            )
                    );
                });

                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.LoadXml(xDocument.ToString());

                return xmlDocument;
            }
        }
    }

    /// <summary>
    ///
    /// </summary>
    public class TestTaskProvider : ITaskProvider
    {
        private List<TaskProxy> _taskList = null;
        static private TestTaskProvider _staticInstance = null;

        private TestTaskProvider()
        {
            if(this._taskList == null)
            {
                this._taskList = new List<TaskProxy>()
                {
                    new TaskProxy()
                    {
                        Name = "Task 1",
                        Description = "This is a Task 1",
                        Owner = "Mark Owen",
                        StartDate = DateTime.Now.AddDays(-1),
                        FinishDate = DateTime.Now.AddDays(1),
                        Status = TaskStatus.Unauthorised
                    },
                    new TaskProxy()
                    {
                        Name = "Task 2",
                        Description = "This is a Task 2",
                        Owner = "Glen Keaton",
                        StartDate = DateTime.Now.AddDays(-2),
                        FinishDate = DateTime.Now.AddDays(2),
                        Status = TaskStatus.Initiated
                    },
                    new TaskProxy()
                    {
                        Name = "Task 3",
                        Description = "This is a Task 3",
                        Owner = "Steve Matthews",
                        StartDate = DateTime.Now.AddDays(-3),
                        FinishDate = DateTime.Now.AddDays(3),
                        Status = TaskStatus.Completed
                    }
                };
            }
        }

        private List<Task> TaskList
        {
            get
            {
                return (TestTaskProvider._staticInstance._taskList
                    .Select(t => (t as Task)).ToList());
            }
        }

        public static ITaskProvider StaticInstance()
        {
            if(TestTaskProvider._staticInstance == null)
            {
                TestTaskProvider._staticInstance = new TestTaskProvider();
            }
            return TestTaskProvider._staticInstance;
        }

        /// <summary>
        /// ITaskProvider implementation
        /// </summary>
        public List<Centrica.Task.Model.Task> GetAllTasks()
        {
            return TaskList;
        }

        public List<Centrica.Task.Model.Task> GetByTemplate(Centrica.Task.Model.Task template)
        {
            return TaskList;
        }

        public Centrica.Task.Model.Task Find(
            Func<Centrica.Task.Model.Task, Boolean> predicate)
        {
            return TaskList
                .First(predicate);
        }

        public void AddNewTask(Centrica.Task.Model.Task task)
        {
            Centrica.Task.Model.Task theTask =
                TestTaskProvider._staticInstance._taskList
                .FirstOrDefault(t => (t.ID == task.ID));
            if (theTask != null)
            {
                task.ID = Guid.NewGuid();
            }
            TestTaskProvider._staticInstance._taskList.Add(new TaskProxy(task));
        }

        public void UpdateTask(Centrica.Task.Model.Task task)
        {
            Centrica.Task.Model.Task taskOrg =
                TestTaskProvider._staticInstance._taskList
                .First(t => (t.ID == task.ID));
            taskOrg.Name = task.Name;
            taskOrg.Description = task.Description;
            taskOrg.Owner = task.Owner;
            taskOrg.StartDate = task.StartDate;
            taskOrg.FinishDate = task.FinishDate;
            taskOrg.Status = task.Status;
        }

        public void RemoveTask(Centrica.Task.Model.Task task)
        {
            TestTaskProvider._staticInstance._taskList.RemoveAll(t => (t.ID == task.ID));
        }
    }
}
